<template>
<div class="mainDiv" style="padding:20px;box-sizing: border-box;flex-direction: column;">
<el-collapse accordion style="height: 100%;overflow: auto;">
	<el-collapse-item v-for="(main,index) in mainbranch" :key="index">
    	<template slot="title">{{main.name}}</template>
    	<el-table :data="main.children"  @row-click="mouseclick" @row-dblclick="mousedblclick" :row-key="getrowkeys" :expand-row-keys="selectbranch">
    		<el-table-column prop="id" label="分类ID"></el-table-column>
    		<el-table-column prop="name" label="分类名称"></el-table-column>
    		<el-table-column prop="addtime" label="添加时间">
				<template slot-scope="scope">
					{{scope.row.addtime|dateTime}}
				</template>
			</el-table-column>
    		<el-table-column label="选择">
				<template slot-scope="scope">
					<el-switch
						v-model="scope.row.isdisable"
						on-text="是"
						off-text="否">
					</el-switch>
				</template>
			</el-table-column>
    	</el-table>
  </el-collapse-item>
</el-collapse>
<el-row style="margin-top:10px !important;text-align:right"> 
	<el-button class="fr" icon="close" type="danger" size="mini" @click="close" style="margin-left:10px;">取消关闭</el-button>
	<el-button class="fr" icon="upload2" type="success" size="mini" @click="submit">确定选择</el-button>
</el-row> 
</div>
</template>
<script>
import C from '@/common/common.js'

export default {
  name: 'goodsCategoryList',
  data () {
    return {
      mainbranch: [],
      subbranch: [],
      selectbranch: []
    }
  },
  methods: {
    getrowkeys: function (row) {
      return row.id
    },
    mouseclick: function (row, event) {
      event.preventDefault()
      this.subbranch.forEach((col) => {
        col.isdisable = false
      })
      row.isdisable = true
      this.selectbranch.push(row)
    },
    mousedblclick: function (row, event) {
      event.preventDefault()
      C.closeBrowserWindow(row)
    },
    getbranch: async function () {
      var that = this
      let data = await C.ajax('/shop/popup/categorylist', this.page, 'post')
      if (data) {
        that.mainbranch = data.p
        that.subbranch = data.c
        for (var i = 0; i < that.mainbranch.length; i++) {
          that.mainbranch[i].children = []
          for (var j = 0; j < that.subbranch.length; j++) {
            if (that.subbranch[j].pid === that.mainbranch[i].id) {
              that.mainbranch[i].children.push(that.subbranch[j])
            }
          }
        }
      }
    },
    close: function () {
      C.closewin()
    },
    submit: function (row) {
      var aaa = {}
      this.subbranch.forEach((res) => {
        if (res.isdisable === true) {
          aaa = res
        }
      })
      if (aaa.id) {
        C.closeBrowserWindow(aaa)
      } else {
        C.error('请选择分类')
        return false
      }
    }
  },
  mounted: function () {
    this.getbranch()
  }
}
</script>

<style>
	
</style>